#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdbool.h>

void PrintArry(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

void Swap(int* p1, int* p2)
{
	int x = *p1;
	*p1 = *p2;
	*p2 = x;
}

//最好：O(N)
//最坏：O(N^2)
void BubbleSort(int* a, int n)
{
	for (int j = 0; j < n; j++)
	{
		bool exchange = true; //假设有序
		//单趟交换
		for (int i = 1; i < n - j; i++)
		{
			if (a[i - 1] > a[i])
			{
				Swap(&a[i - 1], &a[i]);
				exchange = false;

			}
		}
		if (exchange == true)
		{
			break;
		}
	}
}

int main()
{
	int a[] = { 1, 5, 3, 2, 3, 6, 7, 4, 9, 8, 0 };
	PrintArry(a, sizeof(a) / sizeof(a[0]));

	BubbleSort(a, sizeof(a) / sizeof(a[0]));
	PrintArry(a, sizeof(a) / sizeof(a[0]));

	return 0;

}